package importxml;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.io.IOException;
import java.util.Vector;


public class Icalendar {
	
	private boolean valid = false;
	private Vector<Event> events; 
	
	public Icalendar(String filename) {
		events = new Vector<Event>();
		File cal = new File(filename);
		BufferedReader input = null;
		try {
			input = new BufferedReader(new FileReader(cal));
			String line = null;
			while ((line = input.readLine()).equals("")) {
				// do nothing...
			}
			if (!(line.equals("BEGIN:VCALENDAR"))) {
				input.close();
				return;
			}
			while ((line = input.readLine()).equals("")) {
				// do nothing...
			}
			if (!(line.equals("VERSION:1.0"))) {
				input.close();
				return;
			}
			while ((line = input.readLine()) != null) {
				if (line.equals("END:VCALENDAR")) {
					valid = true;
					for (int i = 0; i < events.size(); i++) {
						if (!events.get(i).isValid()) {
							valid = false;
						}
					}
					return;
				}
				else if (line.equals("BEGIN:VEVENT")) {
					Vector<String> lines = new Vector<String>();
					lines.add(line);
					while (!line.equals("END:VEVENT")) {
						line = input.readLine();
						if (line == null) {
							return;
						}
						lines.add(line);
					}
					Event event = new Event(lines);
					events.add(event);
				}
			}
		} 
		catch (IOException e) {
			e.printStackTrace();
		}
	}
	
	public Event getEvent(int index) {
		return events.get(index);
	}
	
	public int getNumberOfEvents() {
		return events.size();
	}
	
	public boolean isValid() {
		return valid;
	}
	
	
	
}
